package com.hellogood.work.evaluate.client.customer;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.work.evaluate.client.common.CommonMessages;
import com.hellogood.work.evaluate.shared.po.Customer;
import com.hellogood.work.evaluate.shared.services.CustomerService;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.BooleanItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;
import com.smartgwt.client.widgets.layout.VLayout;

public class ModifyWindow extends Window {
	private CustomerTab tab = null;
	private boolean isModify = false;
	private String usercode = null;
	private String username = null;
	private TextItem codeItem = null;
	private TextItem nameItem = null;

	private SelectItem productItem = null;
	private TextAreaItem custaddItem = null;
	private TextItem phoneItem = null;
	private TextItem serveripItem = null;
	private TextItem domainItem = null;
	private BooleanItem vpnItem = null;
	private TextItem contactItem = null;
	private TextItem remarkItem = null;

	private Button saveBtn = null;
	private Label msg = null;
	private DynamicForm form = null;

	public ModifyWindow(CustomerTab tab, String usercode, String username, boolean isModify) {
		this.tab = tab;
		this.isModify = isModify;
		this.usercode = usercode;
		this.username = username;
		initView();
	}

	private void initView() {
		LengthRangeValidator length5Validator = new LengthRangeValidator();
		length5Validator.setMin(1);
		length5Validator.setMax(5);
		length5Validator.setErrorMessage(CommonMessages.MaxLength5);

		codeItem = new TextItem("code", "客户编号");
		codeItem.setCanEdit(false);
		codeItem.setLength(4);
		nameItem = new TextItem("name", "客户名称");
		nameItem.setRequired(true);
		nameItem.setLength(10);
		nameItem.setValidators(length5Validator);

		productItem = new SelectItem("product", "产品");
		custaddItem = new TextAreaItem("custadd", "地址");
		custaddItem.setHeight(40);
		phoneItem = new TextItem("phon", "电话号码");
		serveripItem = new TextItem("serverip", "服务器IP");
		domainItem = new TextItem("domain", "远程域名");
		vpnItem = new BooleanItem("vpn", "是否有VPN");
		contactItem = new TextItem("contact", "联系人");
		remarkItem = new TextItem("remark", "备注");

		if (isModify) {
			codeItem.setValue(usercode);
			nameItem.setValue(username);
			setTitle("修改客户");
		} else {
			codeItem.setRequired(true);
			codeItem.setCanEdit(true);
			setTitle("添加客户");
		}

		nameItem.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName() != null && event.getKeyName().equals("Enter")) {
					saveBtn.focus();
				}
			}
		});

		form = new DynamicForm();
		form.setSize("300", "300");
		form.setFields(codeItem, nameItem, productItem, custaddItem, phoneItem, serveripItem, domainItem, vpnItem, contactItem, remarkItem);

		saveBtn = new Button("保存");
		saveBtn.addClickHandler(new SaveBtnClickHandler());

		msg = new Label();
		msg.setSize("350", "30");

		VLayout layout = new VLayout();
		layout.addMember(form);
		layout.addMember(saveBtn);
		layout.addMember(msg);
		layout.setTop(50);
		layout.setLeft(15);
		addChild(layout);
	}

	public TextItem getTextItem() {
		return codeItem;
	}

	private class SaveBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			if (form.validate()) {
				String custcode = codeItem.getValue() == null ? null : codeItem.getValue().toString().trim();
				String custname = nameItem.getValue() == null ? null : nameItem.getValue().toString().trim();
				String procode = productItem.getValue() == null ? "" : productItem.getValue().toString().trim();
				String proname = productItem.getValue() == null ? "" : productItem.getValue().toString().trim();
				String custadd = custaddItem.getValue() == null ? "" : custaddItem.getValue().toString().trim();
				String phone = phoneItem.getValue() == null ? "" : phoneItem.getValue().toString().trim();
				String serverip = serveripItem.getValue() == null ? "" : serveripItem.getValue().toString().trim();
				String domain = domainItem.getValue() == null ? "" : domainItem.getValue().toString().trim();
				boolean vpn = (vpnItem.getValue() != null && Boolean.valueOf(vpnItem.getValue().toString()) == Boolean.TRUE) ? true : false;
				String contact = contactItem.getValue() == null ? "" : contactItem.getValue().toString().trim();
				String remark = remarkItem.getValue() == null ? "" : remarkItem.getValue().toString().trim();
				if (custcode != null && custname != null) {
					Customer customer = new Customer();
					customer.setCustomerNo(custcode);
					customer.setCustomerName(custname);
					customer.setProductCode(procode);
					customer.setProductName(proname);
					customer.setAddress(custadd);
					customer.setTelephone(phone);
					customer.setServerIP(serverip);
					customer.setDomain(domain);
					customer.setHasVPN(vpn);
					customer.setContact(contact);
					customer.setRemark(remark);
					CustomerService.Util.getInstance().modify(customer, ModifyWindow.this.isModify, new ModifyAsyncCallback());
				} else {
					msg.setContents(CommonMessages.CodeNameInputError);
				}
			}
		}

	}

	private class ModifyAsyncCallback implements AsyncCallback<String> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(String result) {
			if (result != null && result.equals("")) {
				msg.setContents(CommonMessages.SaveSuccess);
				ModifyWindow.this.tab.initData();
				if (isModify) {
					destroy();
				} else {
					codeItem.setValue("");
					nameItem.setValue("");
					saveBtn.setTitle(CommonMessages.CreateAgain);
				}
			} else {
				msg.setContents(CommonMessages.SaveFailed + " " + result);
			}
		}
	}

}
